package com.spider.UI;

import com.spider.bean.Employee;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EditEmployeeUI extends JDialog {
    private JTextField idField;
    private JTextField nameField;
    private JComboBox<String> sexComboBox;
    private JTextField ageField;
    private JTextField phoneField;
    private JTextField positionField;
    private JTextField entryDateField;
    private JTextField salaryField;
    private JTextField departmentField;

    private JButton saveButton;
    private JButton cancelButton;

    private EmployeeManagerUI parentUI;
    private Employee employee;
    private int selectedRow;

    public EditEmployeeUI(EmployeeManagerUI parent, int selectedRow) {
        super(parent, "编辑员工信息", true);
        this.parentUI = parent;
        this.selectedRow = selectedRow;

        // 获取选中的员工对象
        this.employee = EmployeeManagerUI.allEmployees.get(selectedRow);

        initialize();
        loadEmployeeData();
        this.setVisible(true);
    }

    private void initialize() {
        this.setSize(400, 500);
        this.setLocationRelativeTo(parentUI);
        this.setLayout(new BorderLayout());

        // 创建主面板
        JPanel mainPanel = new JPanel(new GridLayout(9, 2, 10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 初始化组件
        idField = new JTextField();
        idField.setEditable(false); // ID不可编辑
        nameField = new JTextField();
        sexComboBox = new JComboBox<>(new String[]{"男", "女"});
        ageField = new JTextField();
        phoneField = new JTextField();
        positionField = new JTextField();
        entryDateField = new JTextField();
        salaryField = new JTextField();
        departmentField = new JTextField();

        // 添加组件到面板
        mainPanel.add(new JLabel("ID:"));
        mainPanel.add(idField);
        mainPanel.add(new JLabel("姓名:"));
        mainPanel.add(nameField);
        mainPanel.add(new JLabel("性别:"));
        mainPanel.add(sexComboBox);
        mainPanel.add(new JLabel("年龄:"));
        mainPanel.add(ageField);
        mainPanel.add(new JLabel("电话:"));
        mainPanel.add(phoneField);
        mainPanel.add(new JLabel("职位:"));
        mainPanel.add(positionField);
        mainPanel.add(new JLabel("入职日期:"));
        mainPanel.add(entryDateField);
        mainPanel.add(new JLabel("薪水:"));
        mainPanel.add(salaryField);
        mainPanel.add(new JLabel("部门:"));
        mainPanel.add(departmentField);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout());
        saveButton = new JButton("保存");
        cancelButton = new JButton("取消");
        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        // 添加事件监听器
        saveButton.addActionListener(new SaveButtonListener());
        cancelButton.addActionListener(e -> dispose());

        this.add(mainPanel, BorderLayout.CENTER);
        this.add(buttonPanel, BorderLayout.SOUTH);
    }

    private void loadEmployeeData() {
        // 将员工数据加载到表单中
        idField.setText(String.valueOf(employee.getId()));
        nameField.setText(employee.getName());
        sexComboBox.setSelectedItem(employee.getSex());
        ageField.setText(String.valueOf(employee.getAge()));
        phoneField.setText(employee.getPhone());
        positionField.setText(employee.getPosition());
        entryDateField.setText(employee.getEntryDate());
        salaryField.setText(String.valueOf(employee.getSalary()));
        departmentField.setText(employee.getDepartment());
    }

    private class SaveButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                // 验证输入
                if (nameField.getText().trim().isEmpty()) {
                    JOptionPane.showMessageDialog(EditEmployeeUI.this, "姓名不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 更新员工对象
                employee.setName(nameField.getText().trim());
                employee.setSex((String) sexComboBox.getSelectedItem());
                employee.setAge(Integer.parseInt(ageField.getText().trim()));
                employee.setPhone(phoneField.getText().trim());
                employee.setPosition(positionField.getText().trim());
                employee.setEntryDate(entryDateField.getText().trim());
                employee.setSalary(Double.parseDouble(salaryField.getText().trim()));
                employee.setDepartment(departmentField.getText().trim());

                // 更新表格中的数据
                updateTableRow();

                JOptionPane.showMessageDialog(EditEmployeeUI.this, "员工信息更新成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();

            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(EditEmployeeUI.this, "年龄和薪水必须是数字!", "输入错误", JOptionPane.ERROR_MESSAGE);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(EditEmployeeUI.this, "保存失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
        }

        private void updateTableRow() {
            // 获取父窗口的表格模型并更新对应的行
            javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) parentUI.getTable().getModel();
            model.setValueAt(employee.getId(), selectedRow, 0);
            model.setValueAt(employee.getName(), selectedRow, 1);
            model.setValueAt(employee.getSex(), selectedRow, 2);
            model.setValueAt(employee.getAge(), selectedRow, 3);
            model.setValueAt(employee.getPhone(), selectedRow, 4);
            model.setValueAt(employee.getPosition(), selectedRow, 5);
            model.setValueAt(employee.getEntryDate(), selectedRow, 6);
            model.setValueAt(employee.getSalary(), selectedRow, 7);
            model.setValueAt(employee.getDepartment(), selectedRow, 8);
        }
    }
}